2021 Kakao Internship

string of numbers and words (Lv. 1)
https://school.programmers.co.kr/learn/courses/30/lessons/81301
#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;
const map<string, string> string_int{
{"one", "1"}, {"two", "2"}, {"three", "3"}, {"four", "4"}, {"five", "5"},
{"six", "6"}, {"seven", "7"}, {"eight", "8"}, {"nine", "9"}, {"zero", "0"}
};
int solution(string s){
string answer;
for(int i=0; i<s.length(); ++i){
if(s[i]>='0' && s[i]<='9') answer+=string(1, s[i]);
else{
string str_ans;
while(string_int.find(str_ans)==string_int.end()){
str_ans+=string(1, s[i]);
i++;
}
i--;
cout<<str_ans<<endl;
answer+=string_int.at(str_ans);
}
}
return stoi(answer);
}
int main(void){
string s1="one4seveneight";
string s2="23four5six7";
string s3="2three45sixseven";
string s4="123";
int result1, result2, result3, result4;
result1=solution(s1);
result2=solution(s2);
result3=solution(s3);
result4=solution(s4);
cout<<"result1: "<<result1<<endl;
cout<<"result2: "<<result2<<endl;
cout<<"result3: "<<result3<<endl;
cout<<"result4: "<<result4<<endl;
return 0;
}
Check social distancing (Lv. 2)
https://school.programmers.co.kr/learn/courses/30/lessons/81302
#include <iostream>
#include <string>
#include <vector>
using namespace std;
const int dx[]={0, 1, 0, 1, 2};
const int dy[]={1, 0, 2, 1, 0};
int room_chk(vector<string> place){
for(int i=0; i<place.size(); ++i){
for(int j=0; j<place[0].length(); ++j){
if(place[i][j]=='P'){
for(int k=0; k<5; ++k){
int ii=i+dx[k], jj=j+dy[k];
if(ii<0 || ii>place.size()-1 || jj<0 || jj>place[0].length()-1) continue;
if(place[ii][jj]=='P'){
if(dx[k]+dy[k]==1) return 0;
else {
if(k==3) {
if(place[i+1][j]!='X' || place[i][j+1]!='X') return 0;
} else if(k==2) {
if(place[i][j+1]!='X') return 0;
} else {
if(place[i+1][j]!='X') return 0;
}
}
}
}
}
}
}
return 1;
}
vector<int> solution(vector<vector<string>> places){
vector<int> answer;
for(vector<string> place: places){
answer.push_back(room_chk(place));
}
return answer;
}
int main(void){
vector<vector<string>> places={
{"POOOP", "OXXOX", "OPXPX", "OOXOX", "POXXP"},
{"POOPX", "OXPXP", "PXXXO", "OXXXO", "OOOPP"},
{"PXOPX", "OXOXP", "OXPOX", "OXXOP", "PXPOX"},
{"OOOXX", "XOOOX", "OOOXX", "OXOOX", "OOOOO"},
{"PXPXP", "XPXPX", "PXPXP", "XPXPX", "PXPXP"}
};
vector<int> result=solution(places);
cout<<"[";
for(int i=0; i<result.size()-1; ++i) cout<<result[i]<<", ";
cout<<result[result.size()-1]<<"]"<<endl;
return 0;
}
Edit cell (Lv. 3)
https://school.programmers.co.kr/learn/courses/30/lessons/81303
#include <iostream>
#include <vector>
#include <string>
#include <stack>
using namespace std;
struct excel{
int n, k;
stack<int> undo;
vector<int> state;
excel(){}
excel(int _n, int _k): n(_n), k(_k) {
state=vector<int>(n, 0);
for(int i=0; i<n; ++i){
state[i]=i;
}
}
void U(int x){
k=k-x>0? k-x: 0;
}
void D(int x){
k=k+x<state.size()-1? k+x: state.size()-1;
}
void C(void){
undo.push(state[k]);
state.erase(state.begin()+k);
if(k==state.size()) k--;
}
void Z(void){
if(!undo.size()) return;
state.push_back(undo.top());
if(k>undo.top()) k++;
undo.pop();
sort(state.begin(), state.end());
}
string ret_result(void){
string result(n, 'X');
for(int i=0; i<state.size(); ++i) result[state[i]]='O';
return result;
}
};
string solution(int n, int k, vector<string> cmd){
string answer="";
excel exc(n, k);
for(int i=0; i<cmd.size(); ++i){
if(cmd[i][0]=='U'){
exc.U( (cmd[i][2]-'0') );
} else if(cmd[i][0]=='D'){
exc.D( (cmd[i][2]-'0') );
} else if(cmd[i][0]=='C'){
exc.C();
} else {
exc.Z();
}
}
answer=exc.ret_result();
return answer;
}
int main(void){
int n1=8, n2=8, k1=2, k2=2;
vector<string> cmd1={
"D 2", "C", "U 3", "C" ,"D 4", "C", "U 2", "Z", "Z"
};
vector<string> cmd2={
"D 2","C","U 3","C","D 4","C","U 2","Z","Z","U 1","C"
};
string result1, result2;
result1=solution(n1, k1, cmd1);
result2=solution(n2, k2, cmd2);
cout<<"result1: "<<result1<<endl;
cout<<"result2: "<<result2<<endl;
}
Escape ladyrinth (Lv. 4)
https://school.programmers.co.kr/learn/courses/30/lessons/81304
#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;
struct maze{
vector<vector<int>> roads;
vector<int> trap;
int now, obj, n, t;
static int min_t;
maze(){}
maze(int _n, vector<vector<int>> _roads, vector<int> _trap, int _start, int _end): n(_n), now(_start), obj(_end), t(0), roads(_roads), trap(_trap) {}
void _trap(int node){
for(int i=0; i<roads.size(); ++i){
if(roads[i][0]==node || roads[i][1]==node){
int tmp=roads[i][1];
roads[i][1]=roads[i][0];
roads[i][0]=tmp;
}
}
}
bool move(int next){
for(int i=0; i<roads.size(); ++i){
if(roads[i][0]==now && roads[i][1]==next){
now=next;
t+=roads[i][2];
if(obj==now){
if(min_t==-1) min_t=t;
else min_t=min_t>t? t: min_t;
return true;
}
if(find(trap.begin(), trap.end(), now)!=trap.end()) _trap(now);
return false;
}
}
perror("NO ROUTE ERROR");
exit(1);
}
vector<int> moveable(void){
vector<int> movelist;
for(int i=0; i<roads.size(); ++i){
if(roads[i][0]==now) movelist.push_back(roads[i][1]);
}
return movelist;
}
int time(void){
return t;
}
};
int maze::min_t=-1;
void recru(maze m, map<int, int> been){
vector<int> list=m.moveable();
maze tmp=m;
if(list.size()==0) return;
for(int i=0; i<list.size(); ++i){
if(been.at(list[i])>2) continue;
m.move(list[i]);
been[list[i]]++;
recru(m, been);
been[list[i]]--;
m=tmp;
}
}
int solution(int n, int start, int end, vector<vector<int>> roads, vector<int> traps){
int answer;
map<int, int> been;
for(int i=0; i<n; ++i){
been[i+1]=0;
}
been[start]=1;
maze m(n, roads, traps, start, end);
recru(m, been);
answer=m.min_t;
return answer;
}
int main(void){
int n1=3, n2=4, start1=1, start2=1, end1=3, end2=4;
vector<vector<int>> roads1={
{1, 2, 2}, {3, 2, 3}
};
vector<vector<int>> roads2={
{1, 2, 1}, {3, 2, 1}, {2, 4, 1}
};
vector<int> traps1={2};
vector<int> traps2={2, 3};
int result1=solution(n1, start1, end1, roads1, traps1);
int result2=solution(n2, start2, end2, roads2, traps2);
cout<<"result1: "<<result1<<endl;
cout<<"result2: "<<result2<<endl;
return 0;
}
Split test site (Lv. 5)
https://school.programmers.co.kr/learn/courses/30/lessons/81305